package zozopo.manual;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.manual.operations.ReplyOS;
import zozopo.manual.operations.ReplyTB;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RelyViewForAjaxAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
	
	private int countPage = 0;
	private int beginPageNum = 0;
	private int pageSize = 10;
	private int currentPage;
	private int rowNum = 0;
	private int subjectId = 0;
	private int pageNum = 1;
	
	public int getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(int subjectId) {
		this.subjectId = subjectId;
	}

	HttpServletRequest request;
	HttpServletResponse response;
	private Map<Object, Object> session;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {	
		if(pageNum < 1){
			pageNum = 1;
		}
		this.pageNum = pageNum;
		
	}

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		response.setCharacterEncoding("GB2312");
		response.setContentType("text/xml; charset=gb2312");
	}
	
	public int getCountPage() {
		return countPage;
	}

	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}

	public int getBeginPageNum() {
		return beginPageNum;
	}

	public void setBeginPageNum(int beginPageNum) {
		this.beginPageNum = beginPageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public String execute(){
	
		ReplyOS replyOS = new ReplyOS();
		UserTB user = (UserTB) session.get("userInfo");
		char limitflag3 = '0';
		if(user != null)
		{
			limitflag3 = user.getUser_limitflag().charAt(3);
		}
		beginPageNum =  (pageNum - 1) * pageSize;
		List<ReplyTB> replyList = replyOS.selectReplyBySubjectId(subjectId, beginPageNum, pageSize);
		try {
			response.getWriter().write("<table width=\"720\" border=\"0\" cellpadding=\"0\"  cellspacing=\"0\" style=\"border:1px solid #ffffff; border:1px solid #ffffff;\" id=\"replyTable\">");
			for(ReplyTB reply:replyList){
				String from = "";
				if(reply.getReplys_kind().equals("REPLY.FROM.OUTSIDE")){
					from = "来自站外 ";
				}
				
			response.getWriter().write(	"<tr bgcolor=\"#e5f3ff\">"+
					"<td ><font color=\"green\">回复时间:"+reply.getReplys_date()+" </font>");
				if(!('0' == limitflag3)){
					response.getWriter().write("<a href=\"javascript:deletereply('"+reply.getReplys_id()+"')\">删除</a>");
				}
			response.getWriter().write("</td>"+
				"<td align=\"right\"><font color=\"green\">" +
				from +
				"作者:"+reply.getReplys_name()+"</font><a href=\"#\">"+""+"</a></td></tr>"+
					"<tr><td>"+
					reply.getReplys_context()+
						"</td><td align=\"right\"></td></tr>" +
					"<tr><td><hr></td><td align=\"right\"><hr></td>"+
					"</tr><tr><td><br></td><td align=\"right\">" +
						"<br></td></tr>");
			}
			response.getWriter().write("</table>");
			response.getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		replyOS.releaseConnection();
		return NONE;
	}
}
